<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
// use app\admin\model\Peisong;
use app\index\model\Myorder;
// use app\index\model\Shoplist;
use app\index\model\Showorder;
use app\index\model\Showshoplist;

class Order extends Controller
{

    //展示订单列表
    public function orderList()
    {
        if (empty(session('username'))) {
            $this->error('请先登录', 'login/login');
        }

        $status = input('post.contrller');
        $str = "true ";
        if(!empty($status) && strcmp("订单状态", $status)){
            $str .= ' and status = "'.$status.'"';
        }
        session("admin_orderContrller", $status);


        $orders = Db::table('orderlist')->where($str)->order('orderID', 'asc')->paginate(10);
        $page = $orders->render();
        $this->assign('page', $page);
        $this->assign('orders', $orders);
        return $this->fetch();
    }

    //展示订单详情 发货
    public function orderDetail()
    {
        if (empty(session('username'))) {
            $this->error('请先登录！', 'adminlogin/login');
        }
        $orderID = input('get.orderID');
        // 订单信息
        $order = Db::table("showorder")->where("orderID", $orderID)->find();
        $this->assign('order', $order);

        $shoplists = Db::table("showshoplist")->where("orderID", $orderID)->select();
        $this->assign("order", $order);
        $this->assign("shoplists", $shoplists);
        return $this->fetch();


        // $peisongs=Peisong::all();
        // $this->assign('peisongs',$peisongs);
        // $orderlists=array();
        // foreach($peisongs as $order){
        //     $shoplistitems=array();
        //     $showshoplists=Db::table('showshoplist')->where('orderID',$order->orderID)->order('orderID desc')->select();
        //     foreach($showshoplists as $shoplist){
        //         array_push($shoplistitems, $shoplist);
        //     }
        //     array_push($orderlists,$shoplistitems);
        // }
        // $this->assign('orderlists', $orderlists);
        // return $this->fetch();
    }

    //发货功能
    public function send()
    {
        $orderID = input('post.orderID/d');
        $order = Myorder::get($orderID);
        $order->kddh = input('post.kddh');
        $order->STATUS = '已发货';
        $order->cltime = date('Y-m-d H:i:s');
        $order->save();
        $this->redirect('order/orderlist');
    }

    //删除订单
    public function orderDelete()
    {
        $order = Myorder::get(input('post.orderID'));
        $order->delete();
        $this->redirect('order/orderList');
    }

    //添加订单 此功能不做
    public function orderAdd()
    {
        return $this->fetch();
    }
}
